package controle.gui.cadastro;

import java.awt.EventQueue;

import javax.swing.DefaultComboBoxModel;
import javax.swing.InputVerifier;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFormattedTextField;
import javax.swing.JInternalFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JTable;

import java.awt.BorderLayout;
import javax.swing.JButton;

import java.awt.Component;
import java.awt.Panel;
import java.awt.Button;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.Statement;
import java.text.ParseException;

import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;
import javax.swing.text.MaskFormatter;

import table.SimpleTableModel;
import table.ClienteTableModel;

import com.toedter.calendar.JDateChooser;

import controle.gui.consulta.JfConsultaEmpresa;

import controle.mascara.Mascara;
import controle.valida.CustomPlainDocument;
import controle.valida.CustomPlainDocumentInteger;
import controle.valida.ValidaDocumento;
import javax.swing.JMenuItem;
import javax.swing.JDesktopPane;
import javax.swing.JScrollPane;
import javax.swing.table.DefaultTableModel;


public class FEmpresa extends JInternalFrame {
	private JPanel contentPane;
	private JTextField tfRSocial;
	private JTextField tfNire;
	JFormattedTextField jfCnpj;
	private JTextField textField_1;
	private JTextField textField_4;
	private JTable table;
	private JTextField textField;
	
	private Statement stmt;
	
	private JTextField textField_2;
	private JTextField textField_3;
	private JTextField textField_5;
	
	private JFormattedTextField jfCep;
	private JFormattedTextField ftObSocial;
	private JTextField textField_6;
	private JTextField textField_7;
	private JTable table_1;
	//private JTextField textField;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					FEmpresa frame = new FEmpresa();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 * @throws ParseException 
	 */
	public FEmpresa() throws ParseException {
	super("Modulo (Cadasro Empresas)",true,true,false,true);
		//setBounds(100, 100, 50, 250);
		
		Panel panel = new Panel();
		getContentPane().add(panel, BorderLayout.CENTER);
		panel.setLayout(null);
		
		JButton btnSair = new JButton("Sair");
		btnSair.setBounds(352, 233, 76, 24);
		panel.add(btnSair);
		
		textField = new JTextField();
		textField.setBounds(12, 20, 158, 18);
		panel.add(textField);
		textField.setColumns(10);
		
		JLabel lblCnpj = new JLabel("Cnpj");
		lblCnpj.setBounds(12, 3, 70, 14);
		panel.add(lblCnpj);
		initComponents();
	}
	
	
	

public void initComponents() throws ParseException{


	setBounds(100, 100, 856, 678);
	contentPane = new JPanel();
	contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
	contentPane.setLayout(new BorderLayout(0, 0));
	
	setContentPane(contentPane);
	
	JPanel panel = new JPanel();
	contentPane.add(panel, BorderLayout.CENTER);
	panel.setLayout(null);
	
	final JPanel panel_1 = new JPanel();
	panel_1.setBounds(0, 76, 828, 201);
	
	panel_1.setBorder(new EtchedBorder(EtchedBorder.RAISED, null, null));
    panel_1.setBorder(javax.swing.BorderFactory.createTitledBorder("Dados Empresa"));
	panel.add(panel_1);
	panel_1.setLayout(null);
	
	JLabel lblNewLabel = new JLabel("Razão Social");
	lblNewLabel.setBounds(179, 23, 138, 14);
	panel_1.add(lblNewLabel);
	
	tfRSocial = new JTextField(new CustomPlainDocument(),"",100);
	tfRSocial.setToolTipText("Quantidade Maxima Aceita 100 Caracters, \r\n'N\u00E3o Permitido Acentua\u00E7\u00E3o'");

	tfRSocial.addKeyListener(new KeyAdapter() {
		
		public void keyReleased(KeyEvent e) {
			tfRSocial.setText(tfRSocial.getText().toUpperCase());
		}
	});
	
	tfRSocial.setBounds(179, 39, 637, 23);
	panel_1.add(tfRSocial);
	tfRSocial.setColumns(10);
	
	JLabel lblCnpj = new JLabel("CNPJ");
	lblCnpj.setBounds(12, 23, 60, 14);
	panel_1.add(lblCnpj);
	
	JLabel lblObjetoSocial = new JLabel("Objeto Social");
	lblObjetoSocial.setBounds(445, 65, 100, 14);
	panel_1.add(lblObjetoSocial);
	
	tfNire = new JTextField(new CustomPlainDocumentInteger(),"",100);
	tfNire.setBounds(568, 80, 114, 23);
	panel_1.add(tfNire);
	tfNire.setColumns(10);
	
	JLabel lblNire = new JLabel("Nire");
	lblNire.setBounds(570, 65, 70, 14);
	panel_1.add(lblNire);
	
    jfCnpj = new JFormattedTextField(Mascara.cnpjMask());
    jfCnpj.setToolTipText("Digite Somente Numeros");
    
    jfCnpj.addFocusListener(new FocusAdapter() {
    	
    	public void focusLost(FocusEvent e) {
    		String cnpjS =jfCnpj.getText().replaceAll(".","").replaceAll("-","").replaceAll("/", "");
        	//new ValidaDocumento();
    		//boolean valida =
    		//		ValidaDocumento.isValidCNPJ(cnpjS);
    		if(ValidaDocumento.isValidCNPJ(cnpjS)){
    			JOptionPane.showMessageDialog(getComponent(0), "cpf correto");
    		}	 
    	}
    });
    jfCnpj.addKeyListener(new KeyAdapter() {
    	
    	public void keyPressed(KeyEvent e) {
    		String cnpjS =jfCnpj.getText().replaceAll(".","").replaceAll("-","").replaceAll("/", "");
        	//new ValidaDocumento();
    		boolean valida = ValidaDocumento.isValidCNPJ(cnpjS);
    		if(valida==true){
    			JOptionPane.showMessageDialog(getComponent(0), "cpf correto");
    			System.out.println(valida);
    		}
    	}
  
    	public void keyReleased(KeyEvent e) {
    		String cnpjS =jfCnpj.getText().replaceAll(".","").replaceAll("-","").replaceAll("/", "");
        	//new ValidaDocumento();
    		//boolean valida =
    		//		ValidaDocumento.isValidCNPJ(cnpjS);
    		if(ValidaDocumento.isValidCNPJ(cnpjS)){
    			JOptionPane.showMessageDialog(getComponent(0), "cpf correto");
    		}	 
    	}
    });
    jfCnpj.addActionListener(new ActionListener() {
    	public void actionPerformed(ActionEvent e) {
    		String cnpjS =jfCnpj.getText().replaceAll(".","").replaceAll("-","").replaceAll("/", "");
        	//new ValidaDocumento();
    		boolean valida = ValidaDocumento.isValidCNPJ(cnpjS);	
    		}
    });
    
	jfCnpj.setBounds(12, 39, 160, 23);
	panel_1.add(jfCnpj);
	
	JPopupMenu popupMenu = new JPopupMenu();
	addPopup(jfCnpj, popupMenu);
	
	JMenuItem mntmConsulta = new JMenuItem("Consulta ");
	mntmConsulta.addMouseListener(new MouseAdapter() {
		@Override
		public void mouseReleased(MouseEvent e) {
			JfConsultaEmpresa consulta = new JfConsultaEmpresa();
			consulta.setVisible(true);
			
			
		}
	});
	popupMenu.add(mntmConsulta);
	
	textField_4 = new JTextField();
	textField_4.setToolTipText("Digite Somente Numeros");
	textField_4.setBounds(12, 80, 136, 23);
	panel_1.add(textField_4);
	textField_4.setColumns(10);
	
	JLabel lblIe = new JLabel("Ie");
	lblIe.setBounds(12, 65, 70, 14);
	panel_1.add(lblIe);
	
    ftObSocial = new JFormattedTextField(Mascara.objSocialMask());
	ftObSocial.setBounds(443, 80, 114, 23);
	panel_1.add(ftObSocial);
	
	JLabel lblIMunicipal = new JLabel("I. Municipal");
	lblIMunicipal.setBounds(151, 64, 117, 14);
	panel_1.add(lblIMunicipal);
	
	textField_1 = new JTextField();
	textField_1.setToolTipText("Inscrição Municipal");
	textField_1.setBounds(151, 80, 160, 23);
	panel_1.add(textField_1);
	textField_1.setColumns(10);
	
	JLabel lblDataRegistro = new JLabel("Data Registro");
	lblDataRegistro.setBounds(313, 64, 120, 14);
	panel_1.add(lblDataRegistro);
	
	JDateChooser dateChooser = new JDateChooser();
	dateChooser.setBounds(313, 80, 120, 23);
	panel_1.add(dateChooser);
	
	JLabel lblCSocial = new JLabel("C. Social");
	lblCSocial.setBounds(693, 65, 70, 14);
	panel_1.add(lblCSocial);
	
	textField_3 = new JTextField();
	textField_3.setBounds(694, 80, 114, 23);
	panel_1.add(textField_3);
	textField_3.setColumns(10);
	
	JComboBox comboBox_1 = new JComboBox();
	comboBox_1.setToolTipText("Selecionar o Tipo do Responsavel");
	comboBox_1.setBounds(12, 126, 177, 23);
	panel_1.add(comboBox_1);
	
	JLabel lblResponsavel = new JLabel("Responsável");
	lblResponsavel.setBounds(12, 108, 100, 14);
	panel_1.add(lblResponsavel);
	
	JComboBox comboBox_2 = new JComboBox();
	comboBox_2.setBounds(201, 126, 562, 23);
	panel_1.add(comboBox_2);
	
	JButton btnScios = new JButton("Sócios +");
	btnScios.setToolTipText("Adicionar S\u00F3cio");
	btnScios.setBounds(12, 161, 117, 24);
	panel_1.add(btnScios);
	
	
	
	JPanel panel_2 = new JPanel();
	panel_2.setBounds(0, 289, 828, 143);
	panel_2.setBorder(new TitledBorder(null, "Dados Endereco", TitledBorder.LEFT, TitledBorder.TOP, null, null));
	panel.add(panel_2);
	panel_2.setLayout(null);
	
	JComboBox comboBox = new JComboBox();
	comboBox.setModel(new DefaultComboBoxModel(new String[] {"", "AVENIDA", "RUA", "BECO"}));
	comboBox.setBounds(10, 29, 140, 23);
	panel_2.add(comboBox);
	
	textField = new JTextField();
	textField.setBounds(162, 29, 649, 23);
	panel_2.add(textField);
	textField.setColumns(10);
	
	textField_2 = new JTextField();
	textField_2.setBounds(10, 72, 89, 23);
	panel_2.add(textField_2);
	textField_2.setColumns(10);
	
	JLabel lblN = new JLabel("N°");
	lblN.setBounds(10, 55, 70, 14);
	panel_2.add(lblN);
	
	JLabel lblBairro = new JLabel("Bairro");
	lblBairro.setBounds(111, 55, 70, 14);
	panel_2.add(lblBairro);
	
	textField_5 = new JTextField();
	textField_5.setBounds(111, 72, 249, 23);
	panel_2.add(textField_5);
	textField_5.setColumns(10);
	
	JLabel lblEstado = new JLabel("Estado");
	lblEstado.setBounds(372, 55, 70, 14);
	panel_2.add(lblEstado);
	
	JComboBox comboBox_3 = new JComboBox();
	comboBox_3.setBounds(372, 72, 75, 23);
	panel_2.add(comboBox_3);
	
	JLabel lblCidade = new JLabel("Cidade");
	lblCidade.setBounds(454, 55, 70, 14);
	panel_2.add(lblCidade);
	
	JComboBox comboBox_4 = new JComboBox();
	comboBox_4.setBounds(459, 72, 227, 23);
	panel_2.add(comboBox_4);
	
	JLabel lblCep = new JLabel("Cep");
	lblCep.setBounds(696, 55, 70, 14);
	panel_2.add(lblCep);
	
	jfCep = new JFormattedTextField(Mascara.cepMask());
	jfCep.setBounds(698, 74, 113, 23);
	panel_2.add(jfCep);
	
	JLabel lblNewLabel_1 = new JLabel("Logradouro");
	lblNewLabel_1.setBounds(10, 15, 89, 14);
	panel_2.add(lblNewLabel_1);
	
	JLabel lblComplemento = new JLabel("Complemento");
	lblComplemento.setBounds(10, 97, 106, 14);
	panel_2.add(lblComplemento);
	
	textField_6 = new JTextField();
	textField_6.setBounds(10, 113, 403, 23);
	panel_2.add(textField_6);
	textField_6.setColumns(10);
	
	JPanel panel_3 = new JPanel();
	panel_3.setBounds(0, 12, 761, 52);
	panel_3.setBorder(new EtchedBorder(EtchedBorder.RAISED, null, null));
	panel.add(panel_3);
	panel_3.setLayout(null);
	
	textField_7 = new JTextField();
	textField_7.setBounds(12, 12, 580, 23);
	panel_3.add(textField_7);
	textField_7.setColumns(10);
	
	JButton btnPesquisar = new JButton("Pesquisar");
	btnPesquisar.setBounds(620, 12, 117, 23);
	panel_3.add(btnPesquisar);
	
	
	ClienteTableModel modelo = new ClienteTableModel();  
	table_1 = new JTable(modelo);
	
	
	
	JScrollPane scrollPane = new JScrollPane(table_1);
	scrollPane.setBounds(818, 444, -805, 101);
	panel.add(scrollPane);
	
	
	
	table_1.setBounds(817, 443, -804, 101);
    scrollPane.add(table_1);
    
    JButton btnSair_1 = new JButton("Sair");
    btnSair_1.addMouseListener(new MouseAdapter() {
    	@Override
    	public void mouseReleased(MouseEvent e) {
    	   dispose();
    	}
    });
    btnSair_1.setBounds(707, 601, 117, 24);
    panel.add(btnSair_1);
	
	
	
//	tabela = new TableEmpresa(getResults());
    //table.setModel(tabela);
	
	
}
private static void addPopup(Component component, final JPopupMenu popup) {
	component.addMouseListener(new MouseAdapter() {
		public void mousePressed(MouseEvent e) {
			if (e.isPopupTrigger()) {
				showMenu(e);
			}
		}
		public void mouseReleased(MouseEvent e) {
			if (e.isPopupTrigger()) {
				showMenu(e);
			}
		}
		private void showMenu(MouseEvent e) {
			popup.show(e.getComponent(), e.getX(), e.getY());
		}
	});
}
}
